package com.parking.order.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 支付工具类配置
 * */
@Data
@Configuration
@ConfigurationProperties(prefix = "alipay.config")
public class AlipayConfiguration {
    /**
     * 支付宝网关
     * */
    private String serverUrl;
    /**
     * APPID唯一标识
     * */
    private String appid;
    /**
     * 应用私钥
     * */
    private String privateKey;
    /**
     * 支付宝公钥
     * */
    private String alipayPublicKey;

    /**
     * 配置支付宝支付请求相关信息
     * 返回参数: serverUrl       支付宝网关
     *          appid           AppId唯一标识
     *          privateKey      应用私钥
     *          alipayPublicKey 支付宝公钥
     *          format          输出格式
     *          charset         字符集格式
     *          signType        加密类型
     * */
    @Bean
    public AlipayClient config() throws Exception {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(serverUrl);
        alipayConfig.setAppId(appid);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setFormat("JSON");
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");

        return new DefaultAlipayClient(alipayConfig);
    }
}
